<template>
    <el-card>
        <template #header>
            <h3>店铺添加</h3>
        </template>
        <el-form label-width="119"  :model="shopData">
            <el-form-item :model="shopData" label="店铺名称" >
                <el-input placeholder="店铺名称" v-model="shopData.shopName"></el-input>
            </el-form-item>
            <el-form-item label="店铺地址">
                <el-input placeholder="店铺地址" v-model="shopData.shopAddress"></el-input>
            </el-form-item>
            <el-form-item label="店铺图片">
                <el-upload class="avatar-uploader" 
                    action="http://127.0.0.1:8080/ele/upload" 
                    :show-file-list="false"
                    :on-success="handleAvatarSuccess">

                    <img v-if="shopData.shopImg" :src="shopData.shopImg" class="avatar" />
                    <el-icon v-else class="avatar-uploader-icon">
                        <Plus />
                    </el-icon>
                </el-upload>
            </el-form-item>
            <el-form-item label="店铺类型">
                <el-select v-model="shopData.shopCategoryId">
                    <el-option v-for="item in shopCategoryList" :label="item.categoryName"
                        :value="item.shopCategoryId"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="营业开始时间">
                <el-time-select v-model="shopData.startTime" start="00:00" step="00:30" end="23:59"
                    placeholder="营业开始时间" />
            </el-form-item>
            <el-form-item label="营业结束时间">
                <el-time-select v-model="shopData.endTime" start="00:00" step="00:30" end="23:59"
                    placeholder="营业结束时间" />
            </el-form-item>
            <el-form-item label="起送费">
                <el-input placeholder="起送费" v-model="shopData.startPrice"></el-input>
            </el-form-item>
            <el-form-item label="配送费">
                <el-input placeholder="配送费" v-model="shopData.deliveryPrice"></el-input>
            </el-form-item>
            <el-form-item label="备注">
                <el-input type="textarea" placeholder="备注" v-model="shopData.remarks"></el-input>
            </el-form-item>
            <el-form-item label="店铺介绍">
                <el-input type="textarea" placeholder="店铺介绍" v-model="shopData.shopExplain"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="add">提交</el-button>
            </el-form-item>
        </el-form>


    </el-card>
</template>

<script>
import { shopCategoryListAPI, shopAddAPI } from "@/api/shop.js";
export default {
    data() {
        return {
            shopData: {},
            shopCategoryList: [],
            //商家登录存储在本地的数据
            businessInfo: {},
        }
    },
    methods:{
        //图片上传之后触发的函数
        handleAvatarSuccess(res){
            this.shopData.shopImg=res;
        },
        //加载店铺分类的数据
        loadShopCategory(){
            shopCategoryListAPI().then(resp=>{
                this.shopCategoryList=resp.data;
            })
        },
        //add按钮
        add() {
        // 需要从本地存储中，获取到商家信息，从其中拿到商家id
        this.shopData.businessId = this.businessInfo.id;
        // 调用axios封装函数，将数据提交给后端
      shopAddAPI(this.shopData).then(resp => { 
        if (resp.data == 1) {
          ElNotification({
            type: "success",
            message: "添加成功",
          });
          this.$router.push("/shopList");
        }
      })
    },

    },
    created(){
        // 从本地存储中，获取商家信息
        let info = localStorage.getItem("businessInfo");
        if(!info){
            // 从本地存储中没有获取到商家信息，说明商家没有登录，跳回登录页面
            this.$router.push("/businessLogin");
            return;
        }
        //说明登录了
        this.businessInfo = JSON.parse(info);
        
        this.loadShopCategory();
    },
    
}

</script>

<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}


</style>